package com.example.myapplication;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CardGameModel {
    private List<Card> cards;
    private int score;
    private int moves;
    private Card firstSelectedCard;
    private Card secondSelectedCard;
    private boolean isChecking;
    private int totalPairs;
    private int matchedPairs;

    public CardGameModel() {
        initializeGame();
    }

    public void initializeGame() {
        cards = new ArrayList<>();
        score = 0;
        moves = 0;
        firstSelectedCard = null;
        secondSelectedCard = null;
        isChecking = false;
        matchedPairs = 0;
        
        // 创建8对卡片（共16张）
        for (int i = 1; i <= 8; i++) {
            cards.add(new Card(i, cards.size()));
            cards.add(new Card(i, cards.size()));
        }
        
        totalPairs = 8;
        
        // 洗牌
        Collections.shuffle(cards);
        
        // 重新设置位置
        for (int i = 0; i < cards.size(); i++) {
            cards.get(i).setPosition(i);
        }
    }

    public void flipCard(int position) {
        if (isChecking || position < 0 || position >= cards.size()) {
            return;
        }
        
        Card card = cards.get(position);
        
        // 如果卡片已经匹配或正面朝上，则忽略
        if (card.isMatched() || card.isFaceUp()) {
            return;
        }
        
        // 翻转卡片
        card.setFaceUp(true);
        moves++;
        
        if (firstSelectedCard == null) {
            // 选择第一张卡片
            firstSelectedCard = card;
        } else if (secondSelectedCard == null) {
            // 选择第二张卡片
            secondSelectedCard = card;
            isChecking = true;
            
            // 检查是否匹配
            if (firstSelectedCard.getValue() == secondSelectedCard.getValue()) {
                // 匹配成功
                firstSelectedCard.setMatched(true);
                secondSelectedCard.setMatched(true);
                matchedPairs++;
                score += 10; // 匹配成功加10分
                
                // 如果所有卡片都匹配，游戏结束
                if (matchedPairs == totalPairs) {
                    score += 50; // 游戏完成奖励50分
                }
                
                resetSelection();
            } else {
                // 匹配失败，稍后翻转回去
                score = Math.max(0, score - 1); // 匹配失败扣1分
            }
        }
    }

    public void resetSelection() {
        firstSelectedCard = null;
        secondSelectedCard = null;
        isChecking = false;
    }

    public void flipBackUnmatchedCards() {
        if (firstSelectedCard != null && !firstSelectedCard.isMatched()) {
            firstSelectedCard.setFaceUp(false);
        }
        if (secondSelectedCard != null && !secondSelectedCard.isMatched()) {
            secondSelectedCard.setFaceUp(false);
        }
        resetSelection();
    }

    public boolean isGameOver() {
        return matchedPairs == totalPairs;
    }

    public List<Card> getCards() {
        return cards;
    }

    public int getScore() {
        return score;
    }

    public int getMoves() {
        return moves;
    }

    public int getMatchedPairs() {
        return matchedPairs;
    }

    public int getTotalPairs() {
        return totalPairs;
    }

    public boolean isChecking() {
        return isChecking;
    }

    public Card getFirstSelectedCard() {
        return firstSelectedCard;
    }

    public Card getSecondSelectedCard() {
        return secondSelectedCard;
    }
}